﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using GenCode128;

namespace Cashier_Barcode
{
    public class BarcodeFactory
    {
        /// <summary>
        /// 生成条形码图片128
        /// </summary>
        /// <param name="data">输入数据</param>
        /// <param name="weight">条码宽度</param>
        /// <param name="height">条码高度</param>
        /// <returns></returns>
        public static Image GenerateBarcode128(String data, int width, int height)
        {
            Image barcodeImg = null;
            try
            {
                barcodeImg = Code128Rendering.MakeBarcodeImage(data, width, height, true);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return barcodeImg;
        }

        /// <summary>
        /// 打印条形码128
        /// </summary>
        /// <param name="g"></param>
        /// <param name="x">X坐标</param>
        /// <param name="y">Y坐标</param>
        /// <param name="isShowTitle">是否显示标题</param>
        /// <param name="title">标题</param>
        /// <param name="data">条形码数据</param>
        /// <param name="width">条形码宽度</param>
        /// <param name="height">条形码高度</param>
        public static void DrawBarcode128(Graphics g, int x, int y, bool isShowTitle, String title, String data, int width, int height)
        {
            try
            {
                Image barcodeImg = Code128Rendering.MakeBarcodeImage(data, width, height, true);

                Rectangle rect = new Rectangle();
                Pen pen = new Pen(Brushes.White, 1);
                StringFormat sf = new StringFormat();
                sf.Alignment = StringAlignment.Near;
                sf.LineAlignment = StringAlignment.Center;
                if (isShowTitle)
                {
                    rect = new Rectangle(x, y, barcodeImg.Width, 16);
                    g.DrawRectangle(pen, rect);
                    g.DrawString(title, new Font("宋体", 9), Brushes.Black, rect, sf);
                    y = y + 16;
                }
                g.DrawImage(barcodeImg, new Point(x - 5, y));

                rect = new Rectangle(x, y + barcodeImg.Height + 2, barcodeImg.Width, 16);
                g.DrawRectangle(pen, rect);

                sf.Alignment = StringAlignment.Center;
                g.DrawString(data, new Font("宋体", 9), Brushes.Black, rect, sf);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
